package br.com.amadeus.android.dao;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import br.com.amadeus.android.entities.Role;
import br.com.amadeus.android.entities.User;

public class UserDAO {
	public static String[] columns = new String[] { UserDAO.ID, UserDAO.LOGIN,
			UserDAO.PASSWORD, UserDAO.NAME, UserDAO.EMAIL, UserDAO.ROLE };

	public static final String ID = "_id";
	public static final String LOGIN = "login";
	public static final String PASSWORD = "password";
	public static final String NAME = "name";
	public static final String EMAIL = "email";
	public static final String ROLE = "role_id";

	private SQLiteDatabase db;

	private static UserDAO instance = new UserDAO();

	public static UserDAO getInstance(Context ctx) {

		if (instance.db == null || !instance.db.isOpen()) {
			instance.db = new DBHelper(ctx).getWritableDatabase();
		}
		return instance;
	}

	private UserDAO() {
	}

	public long save(User user) {
		long id = user.getId();
		if (id == 0) {
			return insert(user);
		} else {
			return update(user);
		}
	}

	private long insert(User user) {
		long id_user = -1;

		db.beginTransaction();
		try {

			ContentValues values = new ContentValues();
			values.put(UserDAO.LOGIN, user.getLogin());
			values.put(UserDAO.PASSWORD, user.getPassword());
			values.put(UserDAO.NAME, user.getName());
			values.put(UserDAO.EMAIL, user.getEmail());
			values.put(UserDAO.ROLE, user.getRole().getId());

			id_user = db.insert(DBHelper.TABLE_USER, null, values);
			
			if (id_user != -1)
				db.setTransactionSuccessful();

		} finally {
			db.endTransaction();
		}

		return id_user;
	}

	private long update(User user) {
		boolean deuErro = false;
		db.beginTransaction();
		try {
			ContentValues values = new ContentValues();
			values.put(UserDAO.LOGIN, user.getLogin());
			values.put(UserDAO.PASSWORD, user.getPassword());
			values.put(UserDAO.NAME, user.getName());
			values.put(UserDAO.EMAIL, user.getEmail());
			values.put(UserDAO.ROLE, user.getRole().getId());

			deuErro = db.update(DBHelper.TABLE_USER, values, UserDAO.ID
					+ " = ?", new String[] { String.valueOf(user.getId()) }) == 0;

			if (!deuErro)
				db.setTransactionSuccessful();

		} finally {
			db.endTransaction();
		}
		return user.getId();
	}

	public void delete(int id) {
		db.beginTransaction();
		try {
			db.delete(DBHelper.TABLE_USER, UserDAO.ID + "=?",
					new String[] { String.valueOf(id) });
			db.setTransactionSuccessful();
		} finally {
			db.endTransaction();
		}
	}
	
	public User getUser(int id){
		Cursor c = db.query(DBHelper.TABLE_USER, null, UserDAO.ID + "=" + id,
				null, null, null, null);
		if (c.getCount() > 0) {
			c.moveToFirst();
			return fillUser(c);
		}
		return null;
	}
	
	protected static User fillUser(Cursor c){
		int id = c.getInt(c.getColumnIndex(UserDAO.ID));
		String login = c.getString(c.getColumnIndex(UserDAO.LOGIN));
		String password = c.getString(c.getColumnIndex(UserDAO.PASSWORD));
		String name = c.getString(c.getColumnIndex(UserDAO.NAME));
		String email = c.getString(c.getColumnIndex(UserDAO.EMAIL));
		Role role = new Role(c.getInt(c
				.getColumnIndex(UserDAO.ROLE)));
		return new User(id, login, password, name, email, role);
	}
	
	public User getUserWithCourseUser(int id){
		Cursor c = db.query(DBHelper.TABLE_USER, null, UserDAO.ID + "=" + id,
				null, null, null, null);
		if (c.getCount() > 0) {
			c.moveToFirst();
			return fillUserWithCourseUser(c);
		}
		return null;
	}
	
	protected User fillUserWithCourseUser(Cursor c){
		int id = c.getInt(c.getColumnIndex(UserDAO.ID));
		String login = c.getString(c.getColumnIndex(UserDAO.LOGIN));
		String password = c.getString(c.getColumnIndex(UserDAO.PASSWORD));
		String name = c.getString(c.getColumnIndex(UserDAO.NAME));
		String email = c.getString(c.getColumnIndex(UserDAO.EMAIL));
		Role role = new Role(c.getInt(c
				.getColumnIndex(UserDAO.ROLE)));
		
		User user = new User(id, login, password, name, email, role);
		
		Cursor rc = db.query(DBHelper.TABLE_ROLE, null, RoleDAO.ID + "=" + user.getRole().getId(),
				null, null, null, null);
		if (rc.getCount() > 0) {
			rc.moveToFirst();
			user.setRole(RoleDAO.fillRole(rc));
		}
		
		Cursor cuc = db
		.query(DBHelper.TABLE_COURSEUSER, null, "user_id = ?",
				new String[] { String.valueOf(user.getId()) }, null,
				null, null);

		cuc.moveToFirst();

		while (!cuc.isAfterLast()) {
			user.getCourseUserList().add(null);
			cuc.moveToNext();
		}
		return user;
	}
	
	/*protected CourseUser fillCourseUser(Cursor c){
		
		
	}*/

}
